package com.fancon.android.gardenjokesx.activity;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.ReportErrors;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.ui.widget.FanconProgressBar;

public class L02Activity extends GardenJokesBaseActivity {

	private TextView mTvMSG;
	private TextView mTvLabel;
	private Button mBtnDone;
	private EditText mEditUsername;
	private int numClick = 1;
	String result = "";
	String mMessage = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.l02_layout);
		mTvMSG = (TextView) findViewById(R.id.l02_tvMSG);
		mTvLabel = (TextView) findViewById(R.id.l02_tvLabelUserName);
		mBtnDone = (Button) findViewById(R.id.l02_btn_Done);
		mEditUsername = (EditText) findViewById(R.id.l02_edit_username_or_email);
		mBtnDone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (numClick >= 2) {
					Intent i = new Intent(getApplicationContext(),
							SignupActivity.class);
					startActivity(i);
				} else {
					String user = mEditUsername.getText().toString();
					if (user.length() == 0) {
						//@binhbt
						showDialog(getString(R.string.ERR5001,
								getString(R.string.l02_label_email_or_username)));
					} else {
						ResetPassRequest request = new ResetPassRequest("L02",
								L02Activity.this, user);
						getGlobalState().getRequestQueue().addRequest(request);
					}
				}
			}
		});
	}

	/**
	 * reset password
	 * 
	 * @param userName
	 * @return
	 * @throws IOException 
	 * @throws NoSuchAlgorithmException 
	 */
	private String resetPassWord(String userName) throws NoSuchAlgorithmException, IOException {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
		nameValuePairs.add(new BasicNameValuePair("user", userName));
		GardenJokesApiConnect api = new GardenJokesApiConnect(L02Activity.this);
		result = api.execGet(getApplicationContext(), Constant.XAD_ROOT+"users/reset_password", nameValuePairs);
		Log.d("json", result);
		ReportErrors report;
		JsonPaser json = new JsonPaser();
		report = json.getReport(result);
		return report.getMessage();
	}

	private class ResetPassRequest extends RequestUI {

		private Activity context;
		private FanconProgressBar mProgressDialog;
		private String username;

		public ResetPassRequest(Object key, Activity activity, String userName) {
			super(key, activity);
			// TODO Auto-generated constructor stub
			context = activity;
			username = userName;
			mProgressDialog = FanconProgressBar.show(context,
					context.getString(R.string.common_loading));
		}

		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			try {
				mMessage = resetPassWord(username);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			try {
				if (!result.equals("")) {
					if (mMessage.equals("API-SUCCESS-200")) {
						numClick++;
						mTvMSG.setText(getString(R.string.MSG_L0002));
						mEditUsername.setVisibility(View.INVISIBLE);
						mTvLabel.setVisibility(View.INVISIBLE);
					} else {
						//@binhbt-comment
						showDialog(getString(R.string.ERR5009,
								getString(R.string.l02_label_email_or_username)));
					}
				}
				mProgressDialog.dismiss();
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}

	}
	/*
	 * show dialog when have a error
	 */
	public void showDialog(String msg) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg).setCancelable(true)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
